<template>
  <div class="sdk-container">
    <div class="sdk-api">
      <MenuComponent @menuSelect="menuSelect" />
    </div>
    <div class="sdk-content">
      <div class="sdk-tabs">
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="说明文档" name="doc"></el-tab-pane>
          <el-tab-pane label="在线演示" name="window"></el-tab-pane>
        </el-tabs>
      </div>
      <div class="sdk-web">
        <div class="sdk-doc" v-show="activeName === 'doc'">
          <DocComponent :codeTemplate="codeTemplate" />
        </div>
        <div class="sdk-window" v-show="activeName === 'window'">
          <div class="sdk-editor">
            <EditorComponent
              ref="editor"
              @codeRun="codeRun"
              :codeTemplate="codeTemplate"
            />
          </div>
          <div class="sdk-html">
            <IframeComponent ref="iframe" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import MenuComponent from "./components/menu.vue";
import EditorComponent from "./components/editor.vue";
import IframeComponent from "./components/iframe.vue";
import DocComponent from "./components/doc.vue";
export default {
  components: { MenuComponent, EditorComponent, IframeComponent, DocComponent },
  data() {
    return {
      codeTemplate: "mainStart",
      activeName: "doc",
      codeFirst: false,
    };
  },
  methods: {
    menuSelect(val) {
      this.codeFirst = false;
      this.codeTemplate = val;
      this.activeName = "doc";
    },
    handleClick() {
      if (!this.codeFirst) {
        this.codeFirst = true;
        this.$refs.editor.codeChange();
      }
    },
    codeChange(code = "mainStart") {
      this.codeTemplate = code;
      // this.activeName = "doc";
    },
    codeRun(options) {
      this.$refs.iframe.codeTemplate = this.codeTemplate;
      this.$nextTick(() => {
        this.$refs.iframe.codeRun(options);
      });
    },
  },
  mounted() {
    this.codeChange();
  },
};
</script>

<style scoped lang="scss">
.sdk-container {
  width: 100vw;
  height: 100%;
  background-color: #fafafa;
  min-width: 1000px;
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-column-gap: 10px;
  overflow: hidden;
  > div {
    position: relative;
    height: 100%;
    box-shadow: 0px 0px 6px rgba(0, 0, 0, 0.1);
  }
  .sdk-api {
    background-color: #f7f9fd;
  }
  .sdk-content {
    height: 100%;
    .sdk-tabs {
      height: 41px;
    }
    .sdk-web {
      height: calc(100% - 41px);
      .sdk-doc {
        width: 100%;
        height: 100%;
        overflow: hidden;
      }
      .sdk-window {
        height: 100%;
        display: grid;
        grid-template-columns: 500px 1fr;
        grid-column-gap: 5px;
        .sdk-editor {
          background-color: #ffffff;
        }
        .sdk-html {
          height: 100%;
        }
      }
    }
  }
}
</style>
